home *** CD-ROM | disk | FTP | other *** search
- unit Strutils;
- { string handling procedures }
- {$IFDEF VER90}
- {$H-}
- {$ENDIF}
- interface
-
- const
- separators = ['.', ',', '!', '?',';',':', '"' ];
-
- type
- charSet = set of char;
-
- procedure firstrestStr( var s, first, rest : string );
- { parse string 's' into the 1st word 'first' and the remainder 'rest' }
- function trimLeftStr( s : string ) : string;
- { remove leading separators from string 's' }
- function trimRightStr( s : string ) : string;
- { remove trailing separators from string 's' }
- function trimEndsStr( s : string ) : string;
- { trim separators from both left and right of string }
-
-
- implementation
- function removeLeadChars( s : string; chars : charSet ) : string;
- { trims string 's' by removing chars in charSet 'chars'
- e.g. charSet might be separators. So if s = ' .hello'
- this function would return: 'hello'. }
- var
- outputS : string;
- i : integer;
- begin
- outputS := s;
- i := 1;
- { count to end of characters found in charSet }
- if not (outputS = '') then
- begin
- while (outputS[i] in chars) do
- inc(i);
- Delete(outputS,1, i-1);
- end;
- removeLeadChars := outputS;
- end;
-
- function removeTrailingChars( s : string; chars : charSet ) : string;
- var
- endOfS : integer;
- cleanS : string;
- begin
- cleanS := '';
- endOfS := length(s);
- { count backward from end of characters found in charSet }
- while (s[endOfS] in chars) and (endOfS <> 0 ) do
- dec(endOfS);
- { then return a copy of the string minus the unwanted trailing chars }
- { unless the string is empty in which case return it as such }
- if endOfS <> 0 then cleanS := copy(s, 1, endOfS);
- removeTrailingChars := cleanS;
- end;
-
-
- function trimLeftStr( s : string ) : string;
- { remove leading separators from string 's' }
- begin
- trimLeftStr := removeLeadChars( s, separators );
- end;
-
- function trimRightStr( s : string ) : string;
- { remove trailing separators from string 's' }
- begin
- trimRightStr := removeTrailingChars( s, separators );
- end;
-
- function trimEndsStr( s : string ) : string;
- { trim separators from both left and right of string }
- begin
- trimEndsStr := trimLeftStr(trimRightStr(s));
- end;
-
- procedure firstrestStr( var s, first, rest : string );
- { Given a string, 's', parse out the first word as 'first' and
- leave the remainder of the string untouched as 'rest' }
- var
- i : integer;
- endOfWord : boolean;
- trimmed_s : string;
- begin
- endOfWord := false;
- i := 1;
- first := '';
- rest := '';
- trimmed_s := trimLeftStr( s );
- { if s is a blank string then don't try to process it }
- if length(trimmed_s) = 0 then endOfWord := true;
- { when a a separator is found, endOfWord is true. So parse
- out first word and rest of string. }
- while not endOfWord do
- begin
- if trimmed_s[i] in separators then
- begin
- endOfWord := true;
- first := copy( trimmed_s, 1, i-1 );
- rest := copy(trimmed_s, i, (length(trimmed_s)-i)+1);
- end
- else inc(i);
- end;
- end;
-
- end.
-